package net.googlecode.demenkov.task5.report;

import net.googlecode.demenkov.task5.model.Manager;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Sorts input Collection of manager and prints it.
 * @author Yura Demenkov
 * @since 12.04.11
 */
public class SortedOut {
    /**
     * Sorts input Collection to personal code and prints report.
     * @param stuff Collection of managers
     */
    public void codeSortedOut(Collection<Manager> stuff) {
        BigDecimal minsalary,maxsalary;
        SalaryComparator salaryComparator = new SalaryComparator();
        IDComparator idComparator = new IDComparator();

        SortedSet<Manager> salarysortedstuff = new TreeSet<Manager>(salaryComparator);
        salarysortedstuff.addAll(stuff);
        minsalary=salarysortedstuff.first().getSalary();
        maxsalary=salarysortedstuff.last().getSalary();
        SortedSet<Manager> idsortedstuff = new TreeSet<Manager>(idComparator);
        idsortedstuff.addAll(stuff);
        for (Manager x : idsortedstuff) {
            if ((x.getSalary()!=minsalary) && (x.getSalary()!=maxsalary))System.out.println(x);
        }
    }

    /**
     * Sorts input Collection to Last name and prints report.
     * @param stuff Collection of managers
     */

    public void nameSortedOut(Collection<Manager> stuff) {
        LastNameComparator lastNameComparator = new LastNameComparator();

        SortedSet<Manager> namesortedstuff = new TreeSet<Manager>(lastNameComparator);
        namesortedstuff.addAll(stuff);
        for (Manager x : namesortedstuff) {
            if (x.getAge() <= 45) {
                System.out.println(x);
            }
        }
    }

    /**
     * Sorts input Collection to personal salary and prints report.
     * @param stuff Collection of managers
     */
    public void salarySortedOut(Collection<Manager> stuff) {
        SalaryComparator salaryComparator = new SalaryComparator();
        BigDecimal prevsalary = new BigDecimal(-1);
        SortedSet<Manager> salarysortedstuff = new TreeSet<Manager>(salaryComparator);
        salarysortedstuff.addAll(stuff);
        for (Manager x : salarysortedstuff) {
            if (x.getSalary().compareTo(prevsalary) != 0) {
                System.out.println(x);
                prevsalary = x.getSalary();
            }
        }
    }

    /**
     * Class-comparator to compare managers to salary.
     */
    class SalaryComparator implements Comparator<Manager> {
        @Override
        public int compare(Manager o1, Manager o2) {
            return (o2.getSalary().compareTo(o1.getSalary()));
        }
    }

    /**
     * Class-comparator to compare managers to personal code.
     */
    class IDComparator implements Comparator<Manager> {
        @Override
        public int compare(Manager o1, Manager o2) {
            return o1.getPersonalID().compareTo(o2.getPersonalID());
        }
    }

    /**
     * Class-comparator to compare managers to Last name.
     */
    class LastNameComparator implements Comparator<Manager> {
        @Override
        public int compare(Manager o1, Manager o2) {
            return o1.getLastName().compareToIgnoreCase(o2.getLastName());
        }
    }
}
